home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / ups / apcd-0.5 / apcd-0 / upsnet.c < prev    next >
C/C++ Source or Header  |  1995-11-07  |  5KB  |  226 lines

  1. /*
  2.  * upsnet.c - Network support for the apcd daemon
  3.  *
  4.  * Copyright (c) 1995 Pavel Korensky
  5.  * All rights reserved.
  6.  *
  7.  * Permission is hereby granted, without written agreement and without
  8.  * license or royalty fees, to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose, provided that the
  10.  * above copyright notice and the following two paragraphs appear in
  11.  * all copies of this software.
  12.  * 
  13.  * IN NO EVENT SHALL PAVEL KORENSKY BE LIABLE TO ANY PARTY FOR
  14.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  15.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF PAVEL
  16.  * KORENSKY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17.  *
  18.  * PAVEL KORENSKY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  19.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  20.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  21.  * ON AN "AS IS" BASIS, AND PAVEL KORENSKY HAS NO OBLIGATION TO
  22.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23.  */
  24.  
  25. /*    
  26.  * Version:
  27.  *    
  28.  * $Id: upsnet.c,v 1.1 1995/11/07 12:41:07 root Exp root $
  29.  *    
  30.  *    
  31.  * History:
  32.  *    
  33.  * $Log: upsnet.c,v $
  34.  * Revision 1.1  1995/11/07  12:41:07  root
  35.  * Initial revision
  36.  *
  37.  *    
  38.  */    
  39.  
  40.  
  41. #include "apcd.h"
  42. #include "version.h"
  43.  
  44. static char *version="$Id: upsnet.c,v 1.1 1995/11/07 12:41:07 root Exp root $";
  45.  
  46. int        slavesocket[MAX_SLAVES];
  47. struct sockaddr_in    slave_adr[MAX_SLAVES];
  48.  
  49. int prepare_slave()    /* Bind socket, fill adress */
  50. {                      
  51.     struct    sockaddr_in    my_adr;
  52.     int    masterlen;
  53.     struct    sockaddr_in    master_adr;
  54.     struct    hostent        *mastent;
  55.     int            c,i;
  56.  
  57.     bzero ((char *) &my_adr, sizeof(my_adr));
  58.     my_adr.sin_family = AF_INET;
  59.     my_adr.sin_addr.s_addr = htonl(INADDR_ANY);
  60.     my_adr.sin_port = htons(UPS_TCP_PORT);
  61.     if(bind(socketfd, (struct sockaddr *) &my_adr, sizeof(my_adr)) < 0) {
  62.         syslog(LOG_ERR,"Can't bind local address");
  63.         return(-1);
  64.     }
  65.  
  66.  
  67.     i = 1;
  68.     c = 0;
  69.     while((c == 0) && (i<=10)) {
  70.         listen(socketfd, 1);
  71.         masterlen = sizeof(master_adr);
  72.         newsocketfd = accept(socketfd, (struct sockaddr *) &master_adr, &masterlen);
  73.         if (newsocketfd < 0) {
  74.             syslog(LOG_ERR,"Accept error %d try %d error",i,errno);
  75.             i++;
  76.         }
  77.         else c = 1;
  78.     }
  79.     if (c == 0) return (-1);
  80.     if ((mastent = gethostbyname(master_name)) == NULL) {
  81.         syslog(LOG_ERR,"Can't resolve master name %s",master_name);
  82.         return(-1);
  83.     }
  84.  
  85.     /* Let's add some basic security */
  86.  
  87.     if (memcmp((void *) &master_adr.sin_addr,(void *)mastent->h_addr,sizeof(struct in_addr)) != 0) {
  88.         syslog(LOG_ERR,"Unauthorised attempt from %s",inet_ntoa(master_adr.sin_addr));
  89.         return(-1);
  90.     }
  91.     return(0);    
  92. }
  93.         
  94.  
  95. int get_master_message(int sfd)            /* Get data from master */
  96. {
  97.     int    i,j,t;
  98.     char    c;
  99.     char    line[MAXLINE];
  100.  
  101.     j=0;
  102.     for(j=0;j<MAXLINE;j++) {
  103.         if((i = read(sfd, &c, 1)) == 1) {
  104.             line[j] = c;
  105.             if (c == '\n') break;
  106.         }
  107.         else break;
  108.     }
  109.     if (line[0] == UPS_ON_LINE) {
  110.         gotpowerok = 1;
  111.         return(0);
  112.     }
  113.     if (line[0] == BATT_LOW) {
  114.         masterbatlow = 1;
  115.         return(0);
  116.     }
  117.     if(sscanf(line,"%d",&t) != 1) {
  118.         gottimeout = 0;
  119.         return (-1);
  120.     }
  121.     gottimeout = 1;
  122.     mastertimeout = (t / 60) - 1;    /* One minute before master */
  123.     return(0);
  124. }
  125.  
  126. int send_to_slaves(int what)          /* Make call to all my slaves */
  127. {
  128.     int    i;
  129.  
  130.     for (i=0;i<num_slaves;i++) send_info(i,what);
  131.     return(0);
  132. }
  133.  
  134. int prepare_master()
  135. {
  136.     struct hostent        *slavent;
  137.     int            i,j,c;
  138.  
  139.     for(i=0;i<num_slaves;i++) {
  140.         if ((slavent = gethostbyname(slaves[i])) == NULL) {
  141.             syslog(LOG_ERR,"Can't resolve slave name %s",slaves[i]);
  142.         }
  143.         else {
  144.             c = 0;
  145.             j = 1;
  146.             while((c == 0) && (j<=30)) {
  147.                 if((slavesocket[i] = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  148.                     syslog(LOG_ERR,"Can't allocate socket");
  149.                     return (-1);
  150.                 }
  151.                 bzero ((char *) &slave_adr[i], sizeof(slave_adr[i]));
  152.                 slave_adr[i].sin_family = AF_INET;
  153.                 memcpy((void *)&slave_adr[i].sin_addr,(void *)slavent->h_addr,sizeof(struct in_addr));
  154.                 slave_adr[i].sin_port = htons(UPS_TCP_PORT);
  155.                 if(connect(slavesocket[i], (struct sockaddr *) &slave_adr[i], sizeof(slave_adr[i])) < 0) {
  156.                     syslog(LOG_INFO,"Can't connect to slave %s %d try",slaves[i],j);
  157.                     close(slavesocket[i]);
  158.                     sleep(10);
  159.                     j++;
  160.                 }
  161.                 else c=1;
  162.             }
  163. #ifdef DEBUGGING
  164.             if (c == 1) syslog(LOG_INFO,"Connected to slave %s",slaves[i]);
  165. #endif
  166.         }
  167.     }
  168.     return(0);
  169. }
  170.  
  171.  
  172. int send_info(int slave, int what)    /* Send the data to the slave */
  173. {
  174.     char            c;
  175.     char            time[10];
  176.  
  177.     if (what == -1) {
  178.         c = UPS_ON_LINE;
  179.         if(write(slavesocket[slave],&c,1) != 1) {
  180.             syslog(LOG_ERR,"Can't write to socket");
  181.             return(-1);
  182.         }
  183. #ifdef DEBUGGING
  184.         syslog(LOG_INFO,"Sent UPS_ON_LINE");
  185. #endif
  186.     }
  187.     if (what == -2) {
  188.         c = BATT_LOW;
  189.         if(write(slavesocket[slave],&c,1) != 1) {
  190.             syslog(LOG_ERR,"Can't write to socket");
  191.             return(-1);
  192.         }
  193. #ifdef DEBUGGING
  194.         syslog(LOG_INFO,"Sent BATT_LOW");
  195. #endif
  196.     }
  197.     if (what >=0) {
  198.         sprintf(time,"%d",what);
  199.         if(write(slavesocket[slave],&time,strlen(time)) != strlen(time)) {
  200.             syslog(LOG_ERR,"Can't write to socket");
  201.             return(-1);
  202.         }
  203. #ifdef DEBUGGING
  204.         syslog(LOG_INFO,"Sent timeout %d",what);
  205. #endif
  206.     }
  207.     c = '\n';
  208.     if(write(slavesocket[slave],&c,1) != 1) {
  209.         syslog(LOG_ERR,"Can't write to socket");
  210.         return(-1);
  211.     }
  212. #ifdef DEBUGGING
  213.     syslog(LOG_INFO,"Sent EOL");
  214. #endif        
  215.     return(0);
  216. }
  217.  
  218.  
  219.  
  220.  
  221.  
  222.         
  223.         
  224.             
  225.  
  226.